package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Diseasetype;
import com.sqx.modules.health.entity.Hospital;
import com.sqx.modules.health.service.IDiseasetypeService;
import com.sqx.modules.health.service.IHospitalService;
import com.sqx.modules.health.service.IUserService;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gxy
 * @since 2022-07-19
 */

@Api(value = "", tags = "", description = "")
@RestController
@RequestMapping("/health/diseasetype")
public class DiseasetypeController {
    @Autowired
    private IDiseasetypeService diseasetypeService;

    @Autowired
    private IHospitalService hospitalService;

    @Autowired
    private IUserService userService;


    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save(@RequestBody Diseasetype diseasetype){
        if (ObjectUtil.isEmpty(diseasetype.getCreatetime())) {
            diseasetype.setCreatetime(LocalDateTime.now());
        }
        return Result.success(diseasetypeService.saveOrUpdate(diseasetype));
    }

    // 根据ID删除
    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete(@PathVariable Integer id){
        return Result.success(diseasetypeService.removeById(id));
    }

    // 根据IDS删除
    @GetMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch(@RequestParam List<Integer> ids){
        return Result.success(diseasetypeService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    public Result findAll(){
        return Result.success(diseasetypeService.list());
    }

    // 查询全部
    @GetMapping("/findByHospital")
    @ApiOperation(value = "按照医院查询疾病类型", notes = "按照医院查询疾病类型")
    public Result findByHospital(@RequestParam(required = false, defaultValue = "") Long hid){
        QueryWrapper<Diseasetype> wrapper = new QueryWrapper<Diseasetype>().eq(ObjectUtil.isNotEmpty(hid) ,"hid", hid);
        return Result.success(diseasetypeService.list(wrapper));
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
    public Result findOne(@RequestParam Integer id){
        return Result.success(diseasetypeService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                           @RequestParam(required = false, defaultValue = "")String type,
                           @RequestParam Integer id){
//        String hid = "41";
        SysUserEntity user = userService.getById(id);
        QueryWrapper<Diseasetype> queryWrapper = new QueryWrapper<Diseasetype>()
                .like(ObjectUtil.isNotEmpty(type), "type", type)
                .eq(ObjectUtil.isNotEmpty(user.getHid()), "hid", user.getHid())
                .orderByDesc("createtime");
        Page<Diseasetype> result = diseasetypeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        result.getRecords().forEach(item -> {
            if (ObjectUtil.isNotEmpty(item.getHid())) {
                Hospital hospital = hospitalService.getById(item.getHid());
                item.setHname(hospital.getName());
            }
        });
        return  Result.success(result);
    }


}

